package com.example.module_home.View.View;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.example.module_home.Adapter.CodeTreeItemHolder;
import com.example.module_home.Entity.CodeInfo;
import com.example.module_home.Entity.CodeTree;
import com.example.module_home.Module.CodeBank.CodeBankManager;
import com.example.module_home.Public.Constant;
import com.example.module_home.R;
import com.example.module_home.Utils.DialogUtil;
import com.example.module_home.View.HomeCodeActivity;
import com.example.module_home.View.HomeMainActivity;
import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.view.AndroidTreeView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈煜 on 2018/5/10.
 */

public class TreeView {
    private Context mContext;
    private AndroidTreeView mTreeView;
    private ViewGroup mContainer;
    private TreeViewParamsListener mParamsListener;

    public TreeView(Context context, ViewGroup container){
        this.mContext = context;
        this.mContainer = container;
    }

    public void init(){
        //设置根节点
        TreeNode root = TreeNode.root();
        CodeBankManager.getInstance(mContext).getCodeTree(new TreeView.CodeTreeBack(root));
    }

    public void setTreeViewParamsListener(TreeViewParamsListener paramsListener) {
        mParamsListener = paramsListener;
    }



    class CodeTreeBack implements CodeBankManager.CodeBankBack<List<CodeTree>>{
        private TreeNode mRoot;
        CodeTreeBack(TreeNode root){
            this.mRoot = root;
        }
        @Override
        public void successful(List<CodeTree> codeTrees) {
            mParamsListener.showCodeTreeInfo(codeTrees);
            fillCodeTree(mRoot,codeTrees);
            mTreeView = new AndroidTreeView(mContext,mRoot);
            mTreeView.setDefaultAnimation(true);
            mTreeView.setDefaultContainerStyle(R.style.TreeNodeStyleCustom);
            mTreeView.setDefaultViewHolder(CodeTreeItemHolder.class);
            mTreeView.setDefaultNodeClickListener(new TreeView.CodeTreeClickListener());
            mTreeView.setDefaultNodeLongClickListener(new TreeView.CodeTreeLongClickListener());
            mContainer.addView(mTreeView.getView());
        }

        @Override
        public void failed(int code, String e) {

        }

        private void fillCodeTree(TreeNode node,List<CodeTree> codeTrees){
            for(CodeTree codeTree:codeTrees){
                String title = codeTree.getText();
                int codeId = codeTree.getId();
                if(!codeTree.isIsTitle()){
                    TreeNode folder = new TreeNode(new CodeTreeItemHolder.IconTreeItem(R.drawable.ic_directory_path,title,codeId,false));
                    node.addChild(folder);
                    if(codeTree.getNodes()!=null){
                        fillCodeTree(folder,codeTree.getNodes());
                    }
                }else {
                    TreeNode file = new TreeNode(new CodeTreeItemHolder.IconTreeItem(R.drawable.ic_directory_file,title,codeId,true));
                    node.addChild(file);
                }
            }
        }
    }

    class CodeTreeClickListener implements TreeNode.TreeNodeClickListener{

        @Override
        public void onClick(TreeNode node, Object value) {
            CodeTreeItemHolder.IconTreeItem item = (CodeTreeItemHolder.IconTreeItem) value;
            if(item.isTitle){
                mParamsListener.showCodeInfo(item.id);
            }
//            if (item.isTitle) {
//                CodeBankManager.getInstance(mContext)
//                        .getCodeInfo(item.id, DialogUtil.showLoadingDialog(mContext), new CodeBankManager.CodeBankBack<CodeInfo>() {
//                            @Override
//                            public void successful(CodeInfo info) {
//                                mParamsListener.showCodeInfo(info);
//                            }
//
//                            @Override
//                            public void failed(int code, String e) {
//                            }
//                        });
//            }
        }
    }

    class CodeTreeLongClickListener implements TreeNode.TreeNodeLongClickListener{

        @Override
        public boolean onLongClick(TreeNode node, Object value) {
            return false;
        }
    }

    public interface TreeViewParamsListener{
        void showCodeInfo(int kindId);
        void showCodeTreeInfo(List<CodeTree> codeTrees);
    }

}
